
var catName ="";
$(document).ready(function() {
			catName = $("#catName").val();
			if(catName != "") {
				cg.catPno="";
			}
			cg.loadData();

			$('.l-toolbar').find('.l-panel-btn').mouseover(function() {
						$(this).addClass("l-panel-btn-over");
					});
			$('.l-toolbar').find('.l-panel-btn').mouseout(function() {
						$(this).removeClass("l-panel-btn-over");
					});
});

var cg = {
	mark : "&nbsp;",
	catPno : "0",
	writeBack : function(data) {
		if (data != "") {
			var obj = $.parseJSON(data);
			if (obj.length > 0) {
				var pId = cg.catPno;
		
				// 加载数据之前先清空
				// 先清空
				if (pId != "0") {
					$("#catTable tr[catPno=" + pId + "]").remove();
				}

				for (var i in obj) {
					var str = '<div class="l-grid-row-cell-inner  cell3">';
					str += '<a href="javascript:showRow(\'' + obj[i].catNo
							+ '\')">查看</a>&nbsp;';
					str += '<a href="javascript:editRow(\'' + obj[i].catNo
							+ '\')">修改</a>&nbsp;';
					str += '<a href="javascript:deleteRow(\'' + obj[i].catNo
							+ '\')">删除</a>';
					var testStr = '<a href="javaScript:getChilds(\''
							+ obj[i].catNo
							+ '\')" class="icClose"><img src="../../res/images/system/grid-detail-close.gif" width="11" height="11"/><span class="cname">'
							+ obj[i].catName + '</span></a>&nbsp;';
					if (pId != "0") {
						var kong = $("#catTable tr[id=" + pId + "]")
								.attr("kong");
						testStr = kong + testStr;
					}

					var tr = $("#catTable tr").eq(0).clone();
					tr
							.find("td")
							.eq(0)
							.html('<div style="margin-left: 20px;text-align: left;" class="l-grid-row-cell-inner cell1">'
									+ testStr + '</div>');
					tr.find("td").eq(1)
							.html('<div class="l-grid-row-cell-inner cell2">'
									+ obj[i].updateTime + '</div>');
					tr.find("td").eq(2).html(str);
					tr.attr("catPno", '' + obj[i].catPno + '');
					tr.attr("id", '' + obj[i].catNo + '');
					tr.attr("kong", "&nbsp;&nbsp;");// 用于分级
					tr.show();

					if (pId != "0") {
						var kong = $("#catTable tr[id=" + pId + "]")
								.attr("kong");
						tr.attr("kong", kong + "&nbsp;&nbsp;");// 用于分级
						$("#catTable tr[id=" + pId + "]").after(tr);
					} else {
						tr.appendTo("#catTable");// 首次加载
					}

					// tr鼠标移入事件
					$(tr).mouseover(function() {
								if (!$(this).hasClass('l-selected')) {
									$(this).css("background-color", "#D9E8FB");
								}
							});

					// tr鼠标移出事件
					$(tr).mouseout(function() {
								if (!$(this).hasClass('l-selected')) {
									$(this).css("background-color", "");
								}
							});

					// tr点击事件
					$(tr).click(function() {
								if ($(this).hasClass("l-selected")) {
									$(this).removeClass("l-selected");
									$(this).css("background-color", "#D9E8FB");
								} else {
									$(this).css("background-color", "");
									$("#catTable tr").removeClass("l-selected");
									$(this).addClass("l-selected");
								};
							});
				}
			}
		}
	},
	loadData : function() {
		var url = basePath + '/product/cat/getAjaxNextCommodityCat.do';
		pageFun.ajaxRequest(url, {
					'catPno' : cg.catPno,'catName' : catName
				}, cg.writeBack);
	}

};

function getChilds(catPno) {
	// 判断是折叠还是展开
	var n = $("#catTable tr[catPno='" + catPno + "']").length;
	var obj = $("#catTable tr[id='" + catPno + "']").find("a:eq(0)");
	var cls = $(obj).attr("class");

	if (cls == "icOpen") {// 折叠
		delItem(catPno);
		$(obj).attr("class", "icClose");
		$(obj).find('img').attr('src',
				'../../res/images/system/grid-detail-close.gif');
	} else {
		$(obj).attr("class", "icOpen");
		$(obj).find('img').attr('src',
				'../../res/images/system/grid-detail-open.gif');
		cg.catPno = catPno;
		catName = "";
		cg.loadData();
	};
}

// //折叠删除
function delItem(catPno) {
	var n = $("#catTable tr[catPno='" + catPno + "']").length;
	if (n > 0) {// 折叠删除
		$("#catTable tr[catPno='" + catPno + "']").each(function() {
					delItem($(this).attr("id"));
					$(this).remove();
				});
	}
}

function getTrs(new_tr) {
	var pid = $(new_tr).attr('id');
	var otrs = $(new_tr).nextUntil().filter(function() {
				if ($(this).attr('catPno') == pid) {
					return $(this);
				}
			});
	return otrs;
};

// 得到子项（递归）
function getTrInfo(new_tr, arry, i) {
	arry[i] = new_tr;
	i++;
	var otrs = getTrs(new_tr);
	if (otrs != null && otrs.length > 0) {
		for (var j = 0; j < otrs.length; j++) {
			arry[i] = otrs[j];
			i++;
			getTrInfo(otrs[j], arry, i);
		};
	};
	return arry;
}

// 查看
function showRow(catNo) {

	if (catNo != null && catNo != '') {
		var url = basePath + '/product/cat/getCommodityCatShow.do?catNo='
				+ catNo;
		url += "&pTabid=" + WIN_TAB.getSelectedTabItemID();
		window.parent.f_addTab('查看分类', '查看分类', url);
	}
}

// 修改
function editRow(catNo) {

	if (catNo != null && catNo != '') {
		var url = basePath + '/product/cat/updateToCommodityCat.do?catNo='
				+ catNo;
		url += "&pTabid=" + WIN_TAB.getSelectedTabItemID();
		window.parent.f_addTab(new Date().getTime(), '修改分类', url);
	}
}

// 新增
var tabid_new = null;
function toAddCategory() {

	if (!tabid_new) {
		tabid_new = new Date().getTime();
	}
	var url = basePath + '/product/cat/addToCommodityCat.do';
	url += "?pTabid=" + WIN_TAB.getSelectedTabItemID();
	window.parent.f_addTab('新增分类', '新增分类', url);

}

// 删除
function deleteRow(catNo) {

	$.ligerDialog.confirm('确定删除', function(yes) {
				if (yes) {
					$.post(basePath + '/product/cat/delectCommodityCat.do', {
								catNo : catNo
							}, function(data) {
								if (data == 'success') {
									window.parent.f_tip('删除成功');
									window.location.reload();
								} else if (data == 'fail') {
									window.parent.f_tip('删除失败');
								}else{
									window.parent.f_tip(data);
								};
							});
				};
			});
}
